Measurement = function (robot) {
	this.distances = getDistances(robot);
	
	this.probabilityFor = function(particle) {
		particleDistances = getDistances(particle);
		var p = 1;
		for (var i = 0; i < this.distances.length; i++) {
			p *= gaussian(particleDistances[i], this.distances[i], SENSOR_NOISE);
		}
		return p;
	}
}

function gaussian(x, mean, stdDev) {
	var a = x - mean;
	return Math.exp(-(a * a) / (2 * stdDev * stdDev)) / (Math.sqrt(2 * Math.PI) * stdDev); 
}

function getDistances(robot) {
	return [getNorthDistance(robot), getEastDistance(robot), getSouthDistance(robot), getWestDistance(robot)];
}

function getNorthDistance(robot) {
	var allWalls = H_WALLS;
	var northWalls = allWalls.filter(function (wall) {
		return wall.pos < robot.y && wall.start < robot.x && wall.end > robot.x;
	});
	if (northWalls.length > 0) {
		var closestWall = northWalls[northWalls.length-1];
		return robot.y - closestWall.pos; 
	} else {
		return 9999999;
	}
}

function getSouthDistance(robot) {
	var allWalls = H_WALLS;
	var southWalls = allWalls.filter(function (wall) {
		return wall.pos > robot.y && wall.start < robot.x && wall.end > robot.x;
	});
	if (southWalls.length > 0) {
		var closestWall = southWalls[0];
		return closestWall.pos - robot.y; 
	} else {
		return 9999999;
	}
}

function getEastDistance(robot) {
	var allWalls = V_WALLS;
	var eastWalls = allWalls.filter(function (wall) {
		return wall.pos > robot.x && wall.start < robot.y && wall.end > robot.y;
	});
	if (eastWalls.length > 0) {
		var closestWall = eastWalls[0];
		return closestWall.pos - robot.x; 
	} else {
		return 9999999;
	}
}

function getWestDistance(robot) {
	var allWalls = V_WALLS;
	var westWalls = allWalls.filter(function (wall) {
		return wall.pos < robot.x && wall.start < robot.y && wall.end > robot.y;
	});
	if (westWalls.length > 0) {
		var closestWall = westWalls[westWalls.length-1];
		return robot.x - closestWall.pos; 
	} else {
		return 9999999;
	}
}